package cn.zlp.hrm.service.impl;

import cn.zlp.hrm.client.ThirdPartyServerClient;
import cn.zlp.hrm.domain.VipBase;
import cn.zlp.hrm.exception.ValidUtils;
import cn.zlp.hrm.mapper.VipBaseMapper;
import cn.zlp.hrm.service.IVerifyCodeService;
import cn.zlp.hrm.util.AjaxResult;
import cn.zlp.hrm.util.UserConstants;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: hrm-parent
 * @author: Mr.Zhang
 * @create: 2022-01-13 09:54
 **/
@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {

    @Autowired
    private VipBaseMapper vipBaseMapper;

    @Autowired
    private ThirdPartyServerClient thirdPartyServerClient;

    @Override
    public AjaxResult sendRegisterSmsCode(Map<String, String> params) {
        String mobile = params.get("mobile");
        String imageCode = params.get("imageCode");
        String imageCodeKey = params.get("imageCodeKey");
        //1.验证 = 空值验证 + 是否注册过的验证
        ValidUtils.assertNotNull(mobile,"请输入正常手机号！");
        ValidUtils.assertNotNull(imageCodeKey,"请输入图片验证码");
        ValidUtils.assertNotNull(imageCode,"请输入图片验证码！");
        // 2.图片验证码校验
        AjaxResult ajaxResult = thirdPartyServerClient.smsVerifyCode(params);
        ValidUtils.assertTrue(ajaxResult.isSuccess(),ajaxResult.getMessage());
        // 3.发送短信验证码
        // 根据电话号码查询用户,看该号码是否被注册
        List<VipBase> users  = vipBaseMapper.selectList(new EntityWrapper<VipBase>().eq("phone", mobile));
        ValidUtils.assertListIsNull(users,"该号码已经注册,请直接登录!!!");
        //获取redis中保存的验证码
        Map<String,String> paramsTmp = new HashMap<>();
        paramsTmp.put("type", UserConstants.REGISTER_CODE_PREFIX);
        paramsTmp.put("codeNum","4");
        paramsTmp.put("expire","3");
        paramsTmp.put("phone",mobile);
        return thirdPartyServerClient.sendSmsVerifyCode(paramsTmp);
    }
}
